package com.xoa.service.attend;

import com.xoa.dao.attend.AttendHolidayMapper;
import com.xoa.dao.attendance.AttendScheduleMapper;
import com.xoa.dao.attendance.UserDutyMapper;
import com.xoa.model.attend.AttendExtraWork;

import com.xoa.model.attendance.UserDuty;
import com.xoa.util.ToJson;
import com.xoa.util.attend.AttendDateUtil;
import com.xoa.util.common.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/*
 * 调休上班
 */
@Service
public class ExtraWorkService {

    @Resource
    private AttendScheduleMapper attendScheduleMapper;

    @Resource
    private AttendHolidayMapper extraworkSetMapper;

    @Resource
    private UserDutyMapper userDutyMapper;

    /**
     * 添加调休上班
     */
    public ToJson<AttendExtraWork> insertExtraWork(AttendExtraWork extraworkSet) {
        ToJson<AttendExtraWork> json = new ToJson<AttendExtraWork>(1, "error");
        try {
            int num = extraworkSetMapper.insertExtraWork(extraworkSet);
            if (num > 0) {
//                调休上班添加成功后排班表增加排班
                List<UserDuty> values = new ArrayList<>();
                List<Integer> uidList = getUidList(extraworkSet);
                for (Integer uid : uidList) {
                    List<String> days = AttendDateUtil.getDays(extraworkSet.getBeginDate(), extraworkSet.getEndDate());
                    for (String str : days) {
                        UserDuty userDuty = new UserDuty();
                        userDuty.setUid(uid);
                        userDuty.setDutyType(extraworkSet.getSetType());
                        userDuty.setDutyDate(str);
                        userDuty.setAddType("2");
                        values.add(userDuty);
                    }
                }
                userDutyMapper.insertUserDutyByValues(values, 2);
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 更新添加调休上班
     */
    public ToJson<AttendExtraWork> updateExtraWoekSet(AttendExtraWork extraworkSet) {
        ToJson<AttendExtraWork> json = new ToJson<AttendExtraWork>(1, "error");
        try {
            AttendExtraWork attendExtraWork = extraworkSetMapper.selectExtraWorkById(extraworkSet.getId());
            if (attendExtraWork != null) {
//                    删除数据
                Map map = new HashMap();
                List<Integer> listUid = getUidList(attendExtraWork);
                map.put("listUid", listUid);
                map.put("dateStart", AttendDateUtil.getNextDate(attendExtraWork.getBeginDate()));
                map.put("dateEnd", attendExtraWork.getEndDate());
                map.put("addType", "2");
                userDutyMapper.deleteUserDutyByUidListAndDate(map);
                userDutyMapper.updateByAddType(map);
            }
            int num = extraworkSetMapper.updateExtraWork(extraworkSet);
            if (num > 0) {
//                    新增数据
                List<UserDuty> values = new ArrayList<>();
                List<Integer> uidList = getUidList(extraworkSet);
                for (Integer uid : uidList) {
                    List<String> days = AttendDateUtil.getDays(extraworkSet.getBeginDate(), extraworkSet.getEndDate());
                    for (String str : days) {
                        UserDuty userDuty = new UserDuty();
                        userDuty.setUid(uid);
                        userDuty.setDutyType(extraworkSet.getSetType());
                        userDuty.setDutyDate(str);
                        userDuty.setAddType("2");
                        values.add(userDuty);
                    }
                }
                userDutyMapper.insertUserDutyByValues(values, 2);
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 删除调休上班
     */
    public ToJson<AttendExtraWork> deleteExtraWorkById(int id) {
        ToJson<AttendExtraWork> json = new ToJson<AttendExtraWork>(1, "error");
        try {

            AttendExtraWork attendExtraWork = extraworkSetMapper.selectExtraWorkById(id);
            int count = extraworkSetMapper.deleteExtraWork(id);
            if (attendExtraWork != null && count > 0) {
                //                    删除数据
                Map map = new HashMap();
                List<Integer> listUid = getUidList(attendExtraWork);
                map.put("listUid", listUid);
                map.put("dateStart", AttendDateUtil.getNextDate(attendExtraWork.getBeginDate()));
                map.put("dateEnd", attendExtraWork.getEndDate());
                map.put("addType", "2");
                userDutyMapper.deleteUserDutyByUidListAndDate(map);
                userDutyMapper.updateByAddType(map);
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 根据ID获取调休上班详情
     */
    public ToJson<AttendExtraWork> getExtraWorkById(int id) {
        ToJson<AttendExtraWork> json = new ToJson<AttendExtraWork>(1, "error");
        try {
            AttendExtraWork attendExtraWork = extraworkSetMapper.selectExtraWorkById(id);
            getdeptNameAndPrivNameAndUserName(attendExtraWork);
            json.setObject(attendExtraWork);
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 获取全部调休上班
     */
    public ToJson<AttendExtraWork> getExtraWorkList() {
        ToJson<AttendExtraWork> json = new ToJson<AttendExtraWork>(1, "error");
        try {
            List<AttendExtraWork> list = extraworkSetMapper.getAttendExtraworkList();
            for (AttendExtraWork attendExtraWork : list) {
                getdeptNameAndPrivNameAndUserName(attendExtraWork);
            }
            json.setObj(list);
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }


    //获得调休上班的的所有uid
    public List<Integer> getUidList(AttendExtraWork attendExtraWork) {
        Set<Integer> uidList = new HashSet<>();
        //获取当前的排班人员
        //部门下
        Map map = new HashMap();
        if (!StringUtils.checkNull(attendExtraWork.getDeptId())) {
            if (!attendExtraWork.getDeptId().equals("ALL_DEPT")) {
                map.put("deptIds", attendExtraWork.getDeptId().split(","));
                List<Integer> uids = attendScheduleMapper.getUidListByDeptIds(map);
                uidList.addAll(uids);
            } else {
                List<Integer> uids = attendScheduleMapper.getUidAll(map);
                uidList.addAll(uids);
            }
        }
        //角色下
        if (!StringUtils.checkNull(attendExtraWork.getPrivId())) {
            map.put("privIds", attendExtraWork.getPrivId().split(","));
            List<Integer> uids = attendScheduleMapper.getUidListByPrivIds(map);
            uidList.addAll(uids);
        }
        //userId
        if (!StringUtils.checkNull(attendExtraWork.getUserId())) {
            String[] userids = attendExtraWork.getUserId().split(",");
            List<Integer> uids = attendScheduleMapper.getUidListByUserIds(userids);
            uidList.addAll(uids);
        }
        return new ArrayList<>(uidList);
    }


    /**
     * 获取id对应的名字
     */
    public void getdeptNameAndPrivNameAndUserName(AttendExtraWork attendExtraWork) {
        if (attendExtraWork != null) {
            //获取部门的name
            if (attendExtraWork.getDeptId() != null && attendExtraWork.getDeptId().length() > 0) {
                if (!attendExtraWork.getDeptId().equals("ALL_DEPT")) {
                    List<String> deptNames = attendScheduleMapper.getDeptNames(attendExtraWork.getDeptId());
                    if (deptNames != null && deptNames.size() > 0) {
                        StringBuffer deptName = new StringBuffer();
                        for (int i = 0; i < deptNames.size(); i++) {
                            deptName.append(deptNames.get(i) + ",");
                        }
                        attendExtraWork.setDeptName(deptName.toString());
                    }
                } else {
                    attendExtraWork.setDeptName("全部部门");
                }
            }

            //获取角色的name
            if (attendExtraWork.getPrivId() != null && attendExtraWork.getPrivId().length() > 0) {
                List<String> privNames = attendScheduleMapper.getprivNames(attendExtraWork.getPrivId());
                if (privNames != null && privNames.size() > 0) {
                    StringBuffer privName = new StringBuffer();
                    for (int i = 0; i < privNames.size(); i++) {
                        privName.append(privNames.get(i) + ",");
                    }
                    attendExtraWork.setPrivName(privName.toString());
                }
            }
            //获取用户的name
            if (attendExtraWork.getUserId() != null && attendExtraWork.getUserId().length() > 0) {
                List<String> userNames = attendScheduleMapper.getuserNames(attendExtraWork.getUserId());
                if (userNames != null && userNames.size() > 0) {
                    StringBuffer userName = new StringBuffer();
                    for (int i = 0; i < userNames.size(); i++) {
                        userName.append(userNames.get(i) + ",");
                    }
                    attendExtraWork.setUserName(userName.toString());
                }
            }
        }
    }
}
